
/*
 * @Author: 啊彪
 * @Date: 2021-05-28 14:44:46
 * @LastEditors: 啊彪
 * @LastEditTime: 2021-10-15 09:22:26
 * @Description: 游戏数据
 * @FilePath: \fkxzg\src\utils\stats\gamedata.ts
 */
import abiao_utils from "abiao_utils"
import user from "../../controllers/user.contorller";
import UserLoginLog from "../../models/log/userLoginLogs.model";
import CumuPay from "../../models/stats/cumuPay.model";
import LoginLog from "../../models/stats/loginLog.model";
import PayPeople from "../../models/stats/payPeople.model";
import Retain from "../../models/stats/retain.model";
import User from "../../models/user/user.model";
import utils from "../utils";

export default class Gamedata {

    constructor() {

    }

    /**
     * 登录日志记录
     */
    static async login_log_record(roleId: number, ip: string) {
        let date = abiao_utils.date(2)
        let GameUserRes: any = await User.findOne({ roleId });
        if (!GameUserRes) {
            return
        }

        let RetainRes: any = await Retain.findOne({ date });
        if (!RetainRes) {
            let info = {
                date,
            }
            await Retain.create(info);
        }
        RetainRes = await Retain.findOne({ date });

        // 新增用户
        let newUser = await LoginLog.findOne({ roleId });
        if (!newUser) {
            await Retain.updateOne({ date }, { DNU: Number(RetainRes.DNU) + 1 });
        }

        // 登录用户记录
        let LoginLogRes: any = await LoginLog.findOne({ roleId, loginDate: date });
        if (!LoginLogRes) {
            let info = {
                roleId,
                nickName: GameUserRes.nickName,
                serverId: GameUserRes.serverId,
                ip,
                loginDate: date,
            }
            await LoginLog.create(info);
            let RetainRes: any = await Retain.findOne({ date });
            await Retain.updateOne({ date }, { DAU: Number(RetainRes.DAU) + 1 });
            let createDate = GameUserRes.createTime.split(' ')[0];
            await Gamedata.retain_user_record(createDate, date);
        } else {
            await LoginLog.updateOne({ roleId, loginDate: date }, { loginNum: Number(LoginLogRes.loginNum) + 1 });
        }


        let loginInfo = {
            roleId,
            nickName: GameUserRes.nickName,
            serverId: GameUserRes.serverId,
            ip,
            loginDateTime: abiao_utils.date()
        }
        await UserLoginLog.create(loginInfo);
    }



    /**
     * 留存用户记录
     * @param createDate 创建日期
     * @param nowDate 当前日期
     */
    static async retain_user_record(createDate: string, nowDate: string) {
        // 距离时间
        let distance: number = utils.dateMinus(createDate, nowDate);
        let RetainRes: any = await Retain.findOne({ date: createDate });
        switch (distance) {
            case 1: // 次留
                let secondStay: number = Number(RetainRes.secondStay) + 1;
                let secondStayRate = (secondStay / RetainRes.DNU * 100).toFixed(2)
                await Retain.updateOne({ date: createDate }, { secondStay, secondStayRate });
                break;
            case 3: // 三留
                let threeStay: number = Number(RetainRes.threeStay) + 1;
                let threeStayRate = (threeStay / RetainRes.DNU * 100).toFixed(2)
                await Retain.updateOne({ date: createDate }, { threeStay, threeStayRate });
                break;
            case 7: // 七留
                let sevenStay: number = Number(RetainRes.sevenStay) + 1;
                let sevenStayRate = (sevenStay / RetainRes.DNU * 100).toFixed(2)
                await Retain.updateOne({ date: createDate }, { sevenStay, sevenStayRate });
                break;
            case 15: // 十五留
                let fifteenStay: number = Number(RetainRes.fifteenStay) + 1;
                let fifteenStayRate = (fifteenStay / RetainRes.DNU * 100).toFixed(2)
                await Retain.updateOne({ date: createDate }, { fifteenStay, fifteenStayRate });
                break;
            case 30: // 三十留
                let thirtyStay: number = Number(RetainRes.thirtyStay) + 1;
                let thirtyStayRate = (thirtyStay / RetainRes.DNU * 100).toFixed(2)
                await Retain.updateOne({ date: createDate }, { thirtyStay, thirtyStayRate });
                break;
        }
    }


    /**
     * 总流水记录
     * @param roleId 角色ID
     * @param amount 金额
     */
    static async totalFlowRecord(roleId: number, amount: number) {
        let date = abiao_utils.date(2);
        let RetainRes: any = await Retain.findOne({ date });
        if (!RetainRes) {
            let info = {
                date,
                totalFlow: amount,
            }
            await Retain.create(info);
        } else {
            await Retain.updateOne({ date }, { totalFlow: Number(RetainRes.totalFlow) + Number(amount) });
        }

        let PayPeopleRes: any = await PayPeople.findOne({ roleId, date });
        if (!PayPeopleRes) {
            let info = {
                roleId,
                date,
            }
            await PayPeople.create(info);
        }
        await Gamedata.pay_data_record()
    }

    /**
     * 付费数据记录
     */
    static async pay_data_record() {
        let date = abiao_utils.date(2);
        let RetainRes: any = await Retain.findOne({ date });
        // 活跃ARPU
        let ARPU = (RetainRes.totalFlow / RetainRes.DAU).toFixed(2);
        // 付费ARPU
        let PayPeopleRes: any = await PayPeople.find({ date });
        let ARPPU = (RetainRes.totalFlow / PayPeopleRes.length).toFixed(2);

        let RATE = (PayPeopleRes.length / RetainRes.DNU * 100).toFixed(2);

        await Retain.updateOne({ date }, { ARPU, ARPPU, RATE });
    }


    /**
     * 累计充值记录
     * @param roleId 角色ID
     * @param money 实付金额
     * @param amount 实际道具价值金额
     */
    static async cumu_pay_record(roleId: number, money: number, amount: number) {
        let userInfo: any = await user.getUserInfo(roleId);
        let CumuPayRes: any = await CumuPay.findOne({ roleId });
        if (!CumuPayRes) {
            let info = {
                roleId,
                nickName: userInfo.nickName,
                serverId: userInfo.serverId,
                totalPayMoney: money,
                toDayPayMoney: money,
                actualMoney: amount,
            }
            await CumuPay.create(info);
        } else {
            let totalPayMoney = Number(CumuPayRes.totalPayMoney) + Number(money);
            let toDayPayMoney = Number(CumuPayRes.toDayPayMoney) + Number(money);
            let actualMoney = Number(CumuPayRes.actualMoney) + Number(amount);
            await CumuPay.updateOne({ roleId }, { nickName: userInfo.nickName, totalPayMoney, toDayPayMoney, actualMoney });
        }

    }
}
